using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class asp_pages_register : System.Web.UI.Page
{
    CustomerDAO daoCustomer = new CustomerDAO();
    PersonDAO daoPerson = new PersonDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        LoadInitData();
    }
    public void LoadInitData()
    {
        string[] months = new string[] {"January","February","March","April","May",
        "June","July","August","September","October","November","December"};
        for (int i = 0; i < months.Length; i++)
        {
            cmbMonth.Items.Add(new ListItem(months[i], Convert.ToString(i + 1)));
        }
        cmbMonth.SelectedIndex = 0;
        int curYear = DateTime.Now.Year;
        for (int i = curYear; i >= 1950; i--)
        {
            cmbYear.Items.Add(i.ToString());
        }
        for (int i = 1; i <= 31; i++)
        {
            cmbDay.Items.Add(new ListItem(i.ToString()));
        }
    }
 

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        Customer cus = new Customer();   
        cus.Name = txtFullname.Text;
        cus.Username = txtUsername.Text;
        cus.Password = txtPassword.Text;
        int day = int.Parse(cmbDay.SelectedItem.ToString());
        int month = int.Parse(cmbMonth.SelectedValue);
        int year = int.Parse(cmbYear.SelectedItem.ToString());
        try
        {
            cus.DateOfBirth = new DateTime(year, month, day);
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Invalid date value";
            return;
        }
        cus.Address = txtAddress.Text;
        cus.City = txtCity.Text;
        cus.Country = txtCountry.Text;
        cus.CreditCard = txtCreditCart.Text;
        cus.Email = txtEmail.Text;
        cus.Phone = txtPhone.Text;
        cus.Role = 3; //customer
       cus.LastLogin = new DateTime(int.Parse(cmbYear.SelectedItem.Text),int.Parse(cmbMonth.SelectedValue),int.Parse(cmbDay.SelectedItem.Text));
        if (daoPerson.Insert(cus) > 0)
        {
            if (daoCustomer.InsertCustomer(cus) > 0)
            {
                Response.Redirect("SuccessedMessage.aspx");
            }
            else lblMessage.Text = "Invalid. Please check your input information.";
        }
        else lblMessage.Text = "Invalid pertion details";

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFullname.Text = "";
        txtAddress.Text = "";
        txtPhone.Text = "";
        txtEmail.Text = "";
        txtCity.Text = "";
        txtCountry.Text = "";
        txtCreditCart.Text = "";
        txtPassword.Text = "";
        txtUsername.Text = "";
        
    }
    protected void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        int sel = int.Parse(cmbMonth.SelectedValue);
        int size = 0;
        int year = int.Parse(cmbYear.SelectedItem.Text);
        if (sel == 1 || sel == 3 || sel == 5 || sel == 7 || sel == 8 || sel == 10 || sel == 12)
        {
            size = 31;
        }
        else if (sel == 2)
        {
            if (year % 4 == 0) size = 29;
            else size = 28;
        }
        else
        {
            size = 30;
        }
        cmbDay.Items.Clear();
        for (int i = 1; i <= size; i++)
        {
            cmbDay.Items.Add(i.ToString());
        }
    }
    protected void cmbDay_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void cmbMonth_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
